home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / STATS_U.C < prev    next >
C/C++ Source or Header  |  1997-07-30  |  8KB  |  318 lines

  1. #include "global.h"
  2. #ifdef STATS_USE
  3. #include "stats.h"
  4. #include "stats_u.h"
  5.  
  6.  
  7. #if !defined(_lint)
  8. static char rcsid[] OPTIONAL = "$Id: stats_u.c,v 1.10 1997/07/31 00:44:20 root Exp root $";
  9. #endif
  10.  
  11. long MiscUsers = 0;
  12.  
  13.  
  14. static struct use_stats usestats;
  15.  
  16. static const char usedailyhdr[] = "%10.10s%-34.34sSERVERS\n\n";
  17. static const char useweeklyhdr[] = "%15.15s%-38.38sSERVERS\n\n";
  18. static const char *BBSstr = "BBS";
  19. static const char usagestr[] = "Usage";
  20. static const char usemonthlyhdr[] = "%14.14s%-40.40sSERVERS\n\n";
  21. static const char useyearlyhdr[] = "%4.4s%-21.21sSERVERS\n\n";
  22.  
  23.  
  24. static struct cmds STATSusagecmds[] = {
  25.     { "clear",    doSTATclear_use,0,    0,    NULLCHAR },
  26.     { "daily",    STATdaily_use,    0,    0,    NULLCHAR },
  27.     { "general",    STATgeneral_use,0,     0,    NULLCHAR },
  28.     { "monthly",    STATmonthly_use,0,     0,    NULLCHAR },
  29.     { "weekly",    STATweekly_use,    0,     0,    NULLCHAR },
  30.     { "yearly",    STATyearly_use,    0,     0,    NULLCHAR },
  31.     { NULLCHAR,    NULL,        0,    0,    NULLCHAR }
  32. };
  33.  
  34.  
  35.  
  36. int
  37. doSTATusage(argc,argv,p)
  38. int argc;
  39. char *argv[];
  40. void *p;
  41. {
  42.     return subcmd(STATSusagecmds,argc,argv,p);
  43. }
  44.  
  45.  
  46. int
  47. doSTATclear_use(argc,argv,p)
  48. int argc OPTIONAL;
  49. char *argv[] OPTIONAL;
  50. void *p OPTIONAL;
  51. {
  52.     if(Curproc->input != Command->input)
  53.         tputs (STAT_cannotclear);
  54.     else    {
  55.         memset (&usestats, 0, sizeof (struct use_stats));
  56.         usestats.days = 1;
  57.         usestats.start = time((time_t *)0);
  58.         savestats_use();
  59.         log (-1, "Clearing connection stats");
  60.     }
  61.     return 0;
  62. }
  63.  
  64.  
  65. int
  66. STATgeneral_use(argc,argv,p)
  67. int argc OPTIONAL;
  68. char *argv[] OPTIONAL;
  69. void *p OPTIONAL;
  70. {
  71. char buf1[26], buf2[26], buf3[36];
  72. int temp, l, tempdivisor;
  73. int highest[2], second[2];
  74.  
  75.     tprintf ("\n%26.26s: %-26.26s%-24.24s\n", "USAGE", "BBS", "SERVERS");
  76.     sprintf (buf1, "%ld", usestats.connects[0]);
  77.     sprintf (buf2, "%ld", usestats.connects[1]);
  78.     sprintf (buf3, "Connects since %6.6s", &(ctime(&usestats.start))[4]);
  79.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", buf3, buf1, buf2);
  80.     sprintf (buf1, "%ld", usestats.dailyconnects[0]);
  81.     sprintf (buf2, "%ld", usestats.dailyconnects[1]);
  82.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Connects since midnight",
  83.         buf1, buf2);
  84.     temp = 0;
  85.     if (usestats.connects[0])    {
  86.         tempdivisor = usestats.connects[0] - usestats.dailyconnects[0];
  87.         if (usestats.days > 1 && tempdivisor)
  88.             temp = (int) ((usestats.usage[0] - usestats.day[0] + (tempdivisor - 1)) / tempdivisor);
  89.         else
  90.             temp = (int) (usestats.usage[0] / usestats.connects[0]);
  91.     }
  92.     if (temp > 599)
  93.         sprintf (buf1, "%d hrs, %d min, %d sec", temp / 600, (temp % 600) / 10, ((temp % 600) % 10) * 6);
  94.     else
  95.         sprintf (buf1, "%d min, %d sec", temp / 10, (temp % 10) * 6);
  96.     temp = 0;
  97.     if (usestats.connects[1])    {
  98.         tempdivisor = usestats.connects[1] - usestats.dailyconnects[1];
  99.         if (usestats.days > 1 && tempdivisor)
  100.             temp = (int) ((usestats.usage[1] - usestats.day[1] + (tempdivisor - 1)) / tempdivisor);
  101.         else
  102.             temp = (int) (usestats.usage[1] / usestats.connects[1]);
  103.     }
  104.     if (temp > 599)
  105.         sprintf (buf2, "%d hrs, %d min, %d sec", temp / 600, (temp % 600) / 10, ((temp % 600) % 10) * 6);
  106.     else
  107.         sprintf (buf2, "%d min, %d sec", temp / 10, (temp % 10) * 6);
  108.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Average time per connect",
  109.         buf1, buf2);
  110.     sprintf (buf1, "%d", (usestats.days > 1) ? (int)((usestats.connects[0] - usestats.dailyconnects[0] + (usestats.days - 2)) / (usestats.days - 1)) : (int)usestats.connects[0]);
  111.     sprintf (buf2, "%d", (usestats.days > 1) ? (int)((usestats.connects[1] - usestats.dailyconnects[1] + (usestats.days - 2)) / (usestats.days - 1)) : (int)usestats.connects[1]);
  112.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Average connects per day",
  113.         buf1, buf2);
  114.     highest[0] = highest[1] = -1;
  115.     second[0] = second[1] = -1;
  116.     for (temp = 0; temp < 24; temp++)
  117.         for (l = 0; l < 2; l++)    {
  118.             if (highest[l] == -1 || usestats.hourly[temp][l] > usestats.hourly[highest[l]][l])    {
  119.                 second[l] = highest[l];
  120.                 highest[l] = temp;
  121.             } else if (second[l] == -1 || usestats.hourly[temp][l] > usestats.hourly[second[l]][l])
  122.                 second[l] = temp;
  123.         }
  124.     sprintf (buf1, "At %02d and %02d o'clock", highest[0], second[0]);
  125.     sprintf (buf2, "At %02d and %02d o'clock", highest[1], second[1]);
  126.     tprintf ("%26.26s: %-26.26s%-24.24s\n\n", "Rush hours", buf1, buf2);
  127.     return 0;
  128. }
  129.  
  130.  
  131. int
  132. STATdaily_use(argc,argv,p)
  133. int argc OPTIONAL;
  134. char *argv[] OPTIONAL;
  135. void *p OPTIONAL;
  136. {
  137.     tprintf (dailyhdr, usagestr);
  138.     tprintf (usedailyhdr, STAT_emptystr,BBSstr);
  139.     doGraph (24, usestats.hourly);
  140.     tputs (STAT_dailytrailer);
  141.     return 0;
  142. }
  143.  
  144.  
  145. int
  146. STATweekly_use(argc,argv,p)
  147. int argc OPTIONAL;
  148. char *argv[] OPTIONAL;
  149. void *p OPTIONAL;
  150. {
  151.     tprintf (weeklyhdr, usagestr);
  152.     tprintf (useweeklyhdr, STAT_emptystr,BBSstr);
  153.     doGraph (7, usestats.daily);
  154.     tputs (STAT_weeklytrailer);
  155.     return 0;
  156. }
  157.  
  158.  
  159. int
  160. STATmonthly_use(argc,argv,p)
  161. int argc OPTIONAL;
  162. char *argv[] OPTIONAL;
  163. void *p OPTIONAL;
  164. {
  165.     tprintf (monthlyhdr, usagestr);
  166.     tprintf (usemonthlyhdr, STAT_emptystr,BBSstr);
  167.     doGraph (31, usestats.monthly);
  168.     tputs (STAT_monthlytrailer);
  169.     return 0;
  170. }
  171.  
  172.  
  173. int
  174. STATyearly_use(argc,argv,p)
  175. int argc OPTIONAL;
  176. char *argv[] OPTIONAL;
  177. void *p OPTIONAL;
  178. {
  179.     tprintf (yearlyhdr, usagestr);
  180.     tprintf (useyearlyhdr, STAT_emptystr,BBSstr);
  181.     doGraph (12, usestats.yearly);
  182.     tputs (STAT_yearlytrailer);
  183.     return 0;
  184. }
  185.  
  186.  
  187. void
  188. STATS_adduse (int which)
  189. {
  190.     if (which < 0 || which > 1)
  191.         return;
  192.     usestats.connects[which]++;
  193.     usestats.dailyconnects[which]++;
  194. }
  195.  
  196.  
  197. void
  198. updatestats_use (void)
  199. {
  200. long count;
  201.  
  202.     usestats.hour[0] += (long) BbsUsers;
  203.     usestats.day[0] += (long) BbsUsers;
  204.     usestats.month[0] += (long) BbsUsers;
  205.     usestats.usage[0] += (long) BbsUsers;
  206.     count = (long) FtpUsers + (long) Tutored + MiscUsers;
  207. #ifdef CONVERS
  208.     count += localConfUsers;
  209. #endif
  210.     usestats.hour[1] += count;
  211.     usestats.day[1] += count;
  212.     usestats.month[1] += count;
  213.     usestats.usage[1] += count;
  214. }
  215.  
  216.  
  217. void
  218. loadstats_use(void)
  219. {
  220. FILE *fp;
  221. char buffer[256];
  222.  
  223.     sprintf (buffer, "%s/usage.dat", STATSDir);
  224.     fp = fopen (buffer, "r");
  225.     if (fp != NULLFILE)    {
  226.         (void) fread (&usestats, sizeof (struct use_stats), 1, fp);
  227.         (void) fclose (fp);
  228.     }
  229. }
  230.  
  231.  
  232. void
  233. savestats_use(void)
  234. {
  235. FILE *fp;
  236. char buffer[256];
  237.  
  238.     sprintf (buffer, "%s/usage.dat", STATSDir);
  239.     fp = fopen (buffer, "w");
  240.     if (fp != NULLFILE)    {
  241.         fwrite (&usestats, sizeof (struct use_stats), 1, fp);
  242.         (void) fclose (fp);
  243.     } else
  244.         log (-1, "Can't open stats file '%s/usage.dat'", STATSDir);
  245. }
  246.  
  247.  
  248. void
  249. newhour_use (int hour)
  250. {
  251.     usestats.hourly[hour][0] = usestats.hour[0];
  252.     usestats.hourly[hour][1] = usestats.hour[1];
  253.     usestats.hour[0] = usestats.hour[1] = 0;
  254. }
  255.  
  256.  
  257. void
  258. newday_use (int day)
  259. {
  260.     usestats.daily[day][0] = usestats.day[0];
  261.     usestats.daily[day][1] = usestats.day[1];
  262. }
  263.  
  264.  
  265. void
  266. endmonthclear_use (int day, int month)
  267. {
  268. int k;
  269. FILE *fp;
  270.  
  271.     /* clear out non-existent days of last month */
  272.     for (k = day; k < 31; k++)
  273.         usestats.monthly[k][0] = usestats.monthly[k][1] = 0L;
  274.     usestats.yearly[month][0] = usestats.month[0];
  275.     usestats.yearly[month][1] = usestats.month[1];
  276.     fp = tmpfile();
  277.     if (fp)    {
  278.         fprintf (fp, "Monthly Usage Stats for the month of %s: %s\n\n", Months[month], Hostname);
  279.         fprintf (fp, "BBS Usage: %d hrs, %d min, %d sec\n",
  280.             (int)(usestats.month[0] / 600L), (int) ((usestats.month[0] % 600L) / 10L), (int)(((usestats.month[0] % 600L) % 10L) * 6L));
  281.         fprintf (fp, "SERVERS Usage: %d hrs, %d min, %d sec\n",
  282.             (int)(usestats.month[1] / 600L), (int) ((usestats.month[1] % 600L) / 10L), (int)(((usestats.month[1] % 600L) % 10L) * 6L));
  283.         rewind (fp);
  284.         (void) rdaemon (fp, NULLCHAR, NULLCHAR, "sysop", "Monthly Usage Stats", 'P', 0);
  285.         (void) fclose (fp);
  286.     }
  287.     usestats.month[0] = usestats.month[1] = 0;
  288. }
  289.  
  290.  
  291. void
  292. endday_use (int day)
  293. {
  294.     usestats.monthly[day][0] = usestats.day[0];
  295.     usestats.monthly[day][1] = usestats.day[1];
  296.     usestats.day[0] = usestats.day[1] = 0;
  297.     usestats.days++;
  298.     usestats.dailyconnects[0] = usestats.dailyconnects[1] = 0;
  299. }
  300.  
  301.  
  302. void
  303. eachcycle_use (time_t now)
  304. {
  305.     usestats.last = now;
  306. }
  307.  
  308.  
  309. void
  310. init_use (time_t now)
  311. {
  312.     usestats.days = 1;
  313.     usestats.start = now;
  314. }
  315.  
  316.  
  317. #endif /* STATS_USE */
  318.